//-----------------------------------------------------------------------------
// Window_ShopNumber
//
// The window for inputting quantity of items to buy or sell on the shop
// screen.

Window_ShopNumber.prototype.initialize = function(x, y, width, height) {
    Window_Selectable.prototype.initialize.call(this, x, y, width, height);
    this._item = null;
    this._max = 1;
    this._price = 0;
    this._number = 1;
    this._currencyUnit = TextManager.currencyUnit;
    this.createButtons();
};

Window_ShopNumber.prototype.setup = function(item, max, price) {
    this._item = item;
    this._max = Math.floor(max);
    this._price = price;
    this._number = 1;
    this.placeButtons();
    this.refresh();
    this.showButtons();
};

Window_ShopNumber.prototype.createButtons = function() {
    var bitmap = ImageManager.loadBitmap('img/system/UI/', 'ButtonSetShop');
    var buttonWidth = 48;
    var buttonHeight = 48;
    this._buttons = [];
    for (var i = 0; i < 5; i++) {
        var button = new Sprite_Button();
        var x = buttonWidth * i;
        var w = buttonWidth * (i === 4 ? 2 : 1);
        button.bitmap = bitmap;
        button.setColdFrame(x, 0, w, buttonHeight);
        button.setHotFrame(x, buttonHeight, w, buttonHeight);
        button.setOnFrame(x, 2*buttonHeight, w, buttonHeight);
        button.visible = false;
        this._buttons.push(button);
        this.addChild(button);
    }
    this._buttons[0].setClickHandler(this.onButtonDown2.bind(this));
    this._buttons[1].setClickHandler(this.onButtonDown.bind(this));
    this._buttons[2].setClickHandler(this.onButtonUp.bind(this));
    this._buttons[3].setClickHandler(this.onButtonUp2.bind(this));
    this._buttons[4].setClickHandler(this.onButtonOk.bind(this));
};

Window_ShopNumber.prototype.placeButtons = function() {
    var numButtons = this._buttons.length;
    var buttonWidth = 51;
    var buttonHeight = 34;

    for (var j = 0; j < numButtons; j++) {
        var button = this._buttons[j];
        switch(j){
            case 0:
                button.x = this.buttonX();
                button.y = this.buttonY() + buttonHeight;
                break;
            case 1:
                button.x = this.buttonX() - buttonWidth;
                button.y = this.buttonY();
                break;
            case 2:
                button.x = this.buttonX() + buttonWidth;
                button.y = this.buttonY();
                break;
            case 3:
                button.x = this.buttonX();
                button.y = this.buttonY() - buttonHeight;
                break;
            case 4:
                button.x = this.contents.width - 96 - 32;
                button.y = this.contents.height - 24;
                break;               
        }
    }
};

Window_ShopNumber.prototype.refresh = function() {
    this.contents.clear();
    this.refreshButtonEnabled();
    this.drawNumber();
    this.drawTotalPrice();
};

Window_ShopNumber.prototype.refreshButtonEnabled = function() {
    if (this.changeNumberEnable(-10))
        this._buttons[0].enabled = true;
    else
        this._buttons[0].enabled = false;
    if (this.changeNumberEnable(-1))
        this._buttons[1].enabled = true;
    else
        this._buttons[1].enabled = false;
    if (this.changeNumberEnable(1))
        this._buttons[2].enabled = true;
    else
        this._buttons[2].enabled = false;
    if (this.changeNumberEnable(10))
        this._buttons[3].enabled = true;
    else
        this._buttons[3].enabled = false;
};

Window_ShopNumber.prototype.drawNumber = function() {
    var x = this.cursorX();
    var y = this.itemY();
    var width = this.cursorWidth()/2 + 16 - this.textPadding()/2;
    this.changeTextColor(this.textColor(0));
    this.setSmallerFont();
    this.contents.fontSize = Yanfly.Param.ItemQuantitySize;
    this.drawText("数量：", 0, this.itemY());
    var itemNumber = Yanfly.Util.toGroup(this._number);
    this.drawText(itemNumber, x, y, width, 'right');
};

Window_ShopNumber.prototype.drawTotalPrice = function() {
    var total = this._price * this._number;
    var x = 0;
    var width = this.cursorX() + this.cursorWidth()/2 + 16 - this.textPadding()/2 + 4;
    this.drawCurrencyValue(total, this._currencyUnit, x, this.priceY(), width);
};

Window_ShopNumber.prototype.itemY = function() {
    return 48;
};
Window_ShopNumber.prototype.priceY = function() {
    return 48 + 96;
};

Window_ShopNumber.prototype.buttonX = function() {
    return this.cursorX() + 0.5 *this.cursorWidth();
};

Window_ShopNumber.prototype.buttonY = function() {
    return this.itemY() + 0.5* this.lineHeight();
};

Window_ShopNumber.prototype.cursorWidth = function() {
    return 64;
};

Window_ShopNumber.prototype.cursorX = function() {
    return this.contentsWidth() - this.cursorWidth() - this.textPadding() - 48;
};

Window_ShopNumber.prototype.changeNumber = function(amount) {
    var lastNumber = this._number;
    this._number = (this._number + amount).clamp(1, this._max);
    if (this._number !== lastNumber) {
        SoundManager.playCursor();
        this.refresh();
    }else{
        SoundManager.playBuzzer();
    }
};

Window_ShopNumber.prototype.changeNumberEnable = function(amount) {
    return this._number !== (this._number + amount).clamp(1, this._max);

};

Window_ShopNumber.prototype.drawCurrencyValue = function (value, unit, wx, wy, ww) {
    this.changeTextColor('rgba(89, 186, 142, 1)');
    this.contents.fontSize = Yanfly.Param.GoldFontSize;
    //wy = (this.height - this.contents.fontSize - this.padding * 2 - this.textPadding() * 2) / 2;

    if (this.usingGoldIcon(unit)) {
        var cx = Window_Base._iconWidth;
    } else {
        var cx = this.textWidth(unit);
    }
    var text = Yanfly.Util.toGroup(value);
    if (this.textWidth(text) > ww - cx) {
        text = Yanfly.Param.GoldOverlap;
    }
    this.setSmallerFont();
    this.contents.fontSize = Yanfly.Param.ItemQuantitySize;
    this.drawText(text, cx + wx, wy, ww - cx - 4, 'right');
    this.resetFontSettings();

    if (this.usingGoldIcon(unit)) {
        this.drawIcon(Yanfly.Icon.Gold, wx, wy + 2);
    } else {
        this.changeTextColor(this.systemColor());
        this.drawText(unit, wx, wy, ww, 'right');
    }
    this.resetFontSettings();
};
